import { Chessman } from "./Chessman";

export default class ChessHorse extends Chessman {
  constructor(chessName, camp, x, y, key, chessId) {
    super(chessName, camp, x, y, key, chessId);
  }

  predictionMove(chessList, path?) {
    const temp = [];
    const res = [];
    temp.push(
      ...[
        [this.x + 1, this.y - 2], // 0 右进2
        [this.x - 1, this.y - 2], // 1 左进2
        [this.x + 2, this.y - 1], // 2 右进1
        [this.x - 2, this.y - 1], // 3 左进1
        [this.x + 1, this.y + 2], // 4 右退2
        [this.x - 1, this.y + 2], // 5 左退2
        [this.x + 2, this.y + 1], // 6 右退1
        [this.x - 2, this.y + 1], // 7 左退1
      ]
    );
    for (const [key, path] of chessList) {
      const { x, y } = path;
      if (y === this.y) {
        if (x + 1 === this.x) {
          temp[7] = null;
          temp[3] = null;
        } else if (x - 1 === this.x) {
          temp[2] = null;
          temp[6] = null;
        }
      } else if (x === this.x) {
        if (y + 1 === this.y) {
          temp[0] = null;
          temp[1] = null;
        } else if (y - 1 === this.y) {
          temp[4] = null;
          temp[5] = null;
        }
      }
    }
    if (this.x + 2 > 8) {
      if (this.x + 1 > 8) {
        temp[0] = null;
        temp[4] = null;
      }
      temp[2] = null;
      temp[6] = null;
    }
    if (this.y + 2 > 9) {
      if (this.y + 1 > 9) {
        temp[6] = null;
        temp[7] = null;
      }
      temp[4] = null;
      temp[5] = null;
    }
    if (this.x - 2 < 0) {
      if (this.x - 1 < 0) {
        temp[1] = null;
        temp[5] = null;
      }
      temp[3] = null;
      temp[7] = null;
    }
    if (this.y - 2 < 0) {
      if (this.y - 1 < 0) {
        temp[2] = null;
        temp[3] = null;
      }
      temp[1] = null;
      temp[0] = null;
    }
    res.push(...this.filterChessman(temp, [...chessList]));
    return { data: this, path: res };
  }
}
